package Dao;

import Bean.Bean;
import DBHelper.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class Dao {public int update(String sql, String[] params) {
    int n=0;
    try(Connection conn = DBHelper.getCon();
        PreparedStatement pstmt = conn.prepareStatement(sql)){
        if(params!=null && params.length>0) {
            for(int i=0;i<params.length;i++) {
                pstmt.setString(i+1, params[i]);
            }
        }
        n = pstmt.executeUpdate();
    }catch (Exception e) {
        e.printStackTrace();
    }

    return n;
}
    public List<Bean> query(String sql, String[] params){
        List<Bean> list = new ArrayList<Bean>();
        try(Connection conn = DBHelper.getCon();
            PreparedStatement pstmt = conn.prepareStatement(sql)){
            if(params!=null && params.length>0) {
                for(int i=0;i<params.length;i++) {
                    pstmt.setString(i+1, params[i]);
                }
            }
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()) {
                Bean sb = new Bean();
                sb.setName(rs.getString("name"));
                sb.setPrice(rs.getDouble("price"));
                sb.setVersions(rs.getString("versions"));
                sb.setColour(rs.getString("colour"));
                list.add(sb);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }
}
